package com.icangya.Utils;

import org.json.JSONObject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import javax.servlet.http.Cookie;
import java.io.*;
import java.net.*;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by wang on 2017/4/10.
 */
public class HttpHelper {
    private final Logger logger = LoggerFactory.getLogger(this.getClass());

    public static JSONObject doPost(String url, Map<String, Object> data) throws Exception {
        String result = "";
        HttpURLConnection conn = null;
        try {
            URL realUrl = new URL(url);
            conn = (HttpURLConnection) realUrl.openConnection();
            conn.setRequestProperty("Content-Type", "application/json;charset=UTF-8");
            conn.setReadTimeout(5000);
            conn.setConnectTimeout(5000);
            conn.setRequestMethod("POST");
            conn.setDoOutput(true);
            conn.connect();
            JSONObject jsonObject = new JSONObject(data);
            StringBuffer queryBuffer = new StringBuffer();
            for (Map.Entry<String, Object> entry : data.entrySet()) {
                queryBuffer.append(entry.getKey() + "=" + entry.getValue() + "&");
            }
            String queryStr = queryBuffer.substring(0, queryBuffer.length() - 1);
            queryStr = jsonObject.toString();
            System.out.println(queryStr);
            PrintWriter writer = new PrintWriter(conn.getOutputStream());
            writer.write(queryStr);
            writer.flush();
            writer.close();
            int code = conn.getResponseCode();
            if (code == 200) {
                InputStream inputStream = conn.getInputStream();
                BufferedReader reader = new BufferedReader((new InputStreamReader(inputStream)));
                String line;
                String str = "";
                StringBuffer buffer = new StringBuffer();
                while ((line = reader.readLine()) != null) {
                    str += line;
                    buffer.append(line);
                }
                System.out.print(buffer.toString());

                return new JSONObject(new String(buffer.toString().getBytes("GBK"), "UTF-8"));
            }
        } finally {
            if (conn != null) {
                conn.disconnect();
            }
        }

        return null;
    }

    public static String doGet(String url, Map<String, Object> params, int type) throws Exception {
        String result = "";
        HttpURLConnection conn = null;
        try {
            StringBuffer queryBuffer = new StringBuffer();
            for (Map.Entry<String, Object> entry : params.entrySet()) {
                queryBuffer.append(entry.getKey() + "=" + URLEncoder.encode((String) entry.getValue(), "UTF-8") + "&");
            }
            String queryStr = queryBuffer.substring(0, queryBuffer.length() - 1);
            url = url + "?" + queryStr;
            System.out.println(url);
            URL realUrl = new URL(url);
            conn = (HttpURLConnection) realUrl.openConnection();
            conn.setRequestProperty("Content-Type", "application/json");
            conn.setRequestProperty("User-Agent","Mozilla/5.0 (Windows NT 10.0; WOW64; rv:46.0) Gecko/20100101 Firefox/46.0");
            conn.setReadTimeout(3000);
            conn.setConnectTimeout(3000);
            conn.setRequestMethod("GET");
            BufferedReader in = new BufferedReader(new InputStreamReader(conn.getInputStream(), "UTF-8"));
            String line;
            while ((line = in.readLine()) != null)
                result = result + line;
        } finally {
            if (conn != null) {
                conn.disconnect();
            }
        }
        return result;
    }


}
